package zcw.com.lib_leet_code;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 朱城委 on 2021/7/16.<br><br>
 *
 * 全排列
 */
public class Topic46 {

    public static void main(String[] args) {
        Topic46 instance = new Topic46();

        int[] array = new int[] {1,2,3};
        System.out.println(instance.permute(array));

        array = new int[] {0, 1};
        System.out.println(instance.permute(array));

        array = new int[] {1};
        System.out.println(instance.permute(array));
    }

    public List<List<Integer>> permute(int[] nums) {
        List<Integer> list = new ArrayList<>();
        List<List<Integer>> result = new ArrayList<>();

        permute(nums, list, result);
        return result;
    }

    private void permute(int[] nums, List<Integer> list, List<List<Integer>> result) {
        if(list.size() == nums.length) {
            result.add(new ArrayList<>(list));
            return ;
        }

        for(int value : nums) {
            if(list.contains(value)) {
                continue;
            }

            list.add(value);
            permute(nums, list, result);
            list.remove(list.size() - 1);
        }
    }
}
